package com.lyf.design.learn.designmodel.command;

/**
 * @AUTHOR LYF
 * @DATE 2022/6/4
 * @VERSION 1.0
 * @DESC
 */
public class TxtEditInsertCommand extends TxtEditCommand{
    String addStr;

    public String getAddStr() {
        return addStr;
    }

    public TxtEditInsertCommand(String addStr) {
        this.addStr = addStr;
    }

    @Override
    void exec() {
        //super.exec();
        // 考虑使用参数注入？则需要，记得保存该操作的属性情况，方便回退
        content+=addStr;
        optList.add(this);
        System.out.println("insert successfully:"+content);
    }

//    void exec(String addStr){
//        exec();
//        content+=addStr;
//        optList.add(this);
//
//    }

    @Override
    void undo() {
        if(optList.size()==0){
            System.out.println("回退到最初位置了...");
            return;
        }
        //super.undo();
        // 注意防止null以及越界等问题---检查，，暂时不写
        content=content.substring(0,content.length()-((TxtEditInsertCommand)optList.
                get(optList.size()-1)).getAddStr().length());

        optList.remove(optList.size()-1);
        System.out.println("insert undo successfully:"+content);
    }


}
